package com.hlhj.carview.aty

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import com.hlhj.carview.BaseAty
import com.hlhj.carview.Iview.NothingView
import com.hlhj.carview.R
import com.hlhj.carview.adapter.ArticalListAdp
import com.hlhj.carview.bean.ArticalBean
import com.hlhj.carview.bean.GiverListBean
import com.hlhj.carview.http.CustomAction
import com.hlhj.carview.http.MyJsonConvert
import com.hlhj.carview.http.Urls
import com.hlhj.carview.presenter.NothingPresenter
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.HttpParams
import com.lzy.okgo.model.Response
import com.lzy.okrx2.adapter.ObservableResponse
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.aty_borrowcar.*
import kotlinx.android.synthetic.main.lo_pubhead.*

/**
 *NeverMore
 *2020/9/14
 **/
class ArticalAty:BaseAty<NothingView,NothingPresenter>(), NothingView {
    private lateinit var adp:ArticalListAdp
    private val datas=ArrayList<ArticalBean.Data>()

    override fun initView() {
        tvTittle.text="最新资讯"
        adp= ArticalListAdp(datas)
        listView.layoutManager=LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
        listView.adapter=adp

    }

    override fun logicStart() {

        val httpParams = HttpParams()
        httpParams.put("uriPath","artical")
        OkGo.post<ArticalBean>(Urls.PubUrl)
                .headers("type",3.toString())
                .params(httpParams)
                .converter(object : MyJsonConvert<ArticalBean>(){})
                .adapt(ObservableResponse<ArticalBean>())
                .subscribeOn(Schedulers.io())
                .subscribe(object : CustomAction<Response<ArticalBean>>(this){
                    override fun httpSuccess(t: Response<ArticalBean>) {
                        t.body().data?.let { it1 -> datas.addAll(it1) }
                        adp.notifyDataSetChanged()
                    }
                })
    }

    override fun setListener() {
        adp.setOnItemClickListener { adapter, view, position ->
            val intent= Intent(this,JudgeAty::class.java)
            intent.putExtra("content",datas[position].content)
            intent.putExtra("time",datas[position].time)
            intent.putExtra("icon",datas[position].thumb)
            intent.putExtra("aid",datas[position]._id?.`$oid`)
            intent.putExtra("title",datas[position].name)
            intent.putExtra("content",datas[position].content)
            startActivity(intent)
        }
    }

    override fun getContentID(): Int {
        return R.layout.aty_borrowcar
    }

    override fun bindView(): NothingView {
        return this
    }

    override fun createPresenter(): NothingPresenter {
        return NothingPresenter()
    }
}